package com.bbx.taxi.client.widget.pop;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Adapter.EditAddressAdapter;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.AddressDB;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class CollecteAddressPop {
	private Context context;
	private LayoutInflater inflater = null;
	private AddressDB db_address;
	public PopupWindow popview;
	public static CollecteAddressPop mCollecteAddressPop;
	private EditAddressAdapter mEditAddressAdapter;
	 List<Map<String, String>> list_collect = new ArrayList<Map<String, String>>();
	public static CollecteAddressPop getInstance(Context context) {
		if (mCollecteAddressPop == null) {
			mCollecteAddressPop = new CollecteAddressPop(context);
			return mCollecteAddressPop;

		} else
			return mCollecteAddressPop;
	}
	public CollecteAddressPop(Context context) {
		this.context = context;
		db_address = new AddressDB(context);
		inflater = LayoutInflater.from(context);
		initPopView();
	}

	public void initPopView() {
		View view = inflater.inflate(R.layout.pop_collecte_address, null);
		  popview = new PopupWindow(view,
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		popview.setBackgroundDrawable(new BitmapDrawable());
		popview.setContentView(view);
		popview.setOutsideTouchable(false);
		popview.setFocusable(true);
		popview.setAnimationStyle(R.style.mypopwindow_anim_style);
		ListView listview = (ListView) view.findViewById(R.id.listview);
		list_collect.addAll(db_address.readAllDB(context,
				Value.ADDRESS_TYPE_COLLECTE));
		mEditAddressAdapter= new EditAddressAdapter(
				context, list_collect, EditAddressAdapter.TYPE_COLLECT,
				db_address);
		listview.setAdapter(mEditAddressAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3){
				if(mOnItemClickListener!=null)mOnItemClickListener.onItemClick(list_collect,arg2);
				onDismiss();
			}
		});
		popview.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				if (!mOnAddressPoPViewStateListenerlist.isEmpty()) {
					for (int i = 0; i < mOnAddressPoPViewStateListenerlist.size(); i++) {
						mOnAddressPoPViewStateListenerlist.get(i).onAddressPopViewState(
								false);
					}
				}
				
			}
		});
	}

	public void onShow(View v) {
		list_collect.clear();
		list_collect.addAll(db_address.readAllDB(context,
				Value.ADDRESS_TYPE_COLLECTE));
		mEditAddressAdapter.notifyDataSetChanged();
		if(list_collect.size()>0){
			popview.showAtLocation(v, Gravity.BOTTOM, 0, 0);
			if (!mOnAddressPoPViewStateListenerlist.isEmpty()) {
				for (int i = 0; i < mOnAddressPoPViewStateListenerlist.size(); i++) {
					mOnAddressPoPViewStateListenerlist.get(i).onAddressPopViewState(
							true);
				}
			}
		}
		else{
			ToastUtil.showToast(context, R.string.no_collecte_address);
		}
		
	}

	public void onDismiss() {
		if (!mOnAddressPoPViewStateListenerlist.isEmpty()) {
			for (int i = 0; i < mOnAddressPoPViewStateListenerlist.size(); i++) {
				mOnAddressPoPViewStateListenerlist.get(i).onAddressPopViewState(
						false);
			}
		}
		popview.dismiss();
	}
	/**
	 * 自定义PopView开关状态监听
	 */
	 public static  ArrayList<OnAddressPoPViewStateListener>  mOnAddressPoPViewStateListenerlist=new ArrayList<OnAddressPoPViewStateListener>();
	/**
	 * 自定义PopView开关状态监听
	 */
	public interface OnAddressPoPViewStateListener {
		public void onAddressPopViewState(boolean isOpen);
	}

	public  static void setOnAddressPoPViewStateListener(OnAddressPoPViewStateListener hOnAddressPoPViewStateListener) {
		mOnAddressPoPViewStateListenerlist.add(hOnAddressPoPViewStateListener);
	}
	public static  void removeOnPhotosPoPViewStateListener(OnAddressPoPViewStateListener hOnAddressPoPViewStateListener){
		mOnAddressPoPViewStateListenerlist.remove(hOnAddressPoPViewStateListener);
	}
	///////////////
	/**
	 * 自定义List点击
	 */
	public OnAddressItemClickListener mOnItemClickListener;

	/**
	 * 自定义List点击
	 */
	public interface OnAddressItemClickListener {
		public void onItemClick(List<Map<String, String>> list,int position);
	}

	public void setOnItemClickListener(OnAddressItemClickListener hOnItemClickListener) {
		mOnItemClickListener = hOnItemClickListener;
	}
}
